package com.buddies.lottery.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AlertDialog;

public class PermissionUtils {

    public static boolean checkPermission(Context context, String permission) {
        return android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M ||
                PermissionChecker.checkSelfPermission(context, permission) == PermissionChecker.PERMISSION_GRANTED;
    }

    public static void requestPermissions(Activity activity, String[] permissions) {
        requestPermissions(activity, permissions, 0);
    }

    public static void requestPermissions(Activity activity, String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }

    public static void askForPermission(final FragmentActivity activity, String tips) {
        new AlertDialog.Builder(activity)
                .setMessage(tips)
                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SystemUtils.jumpAppDetailSettings(activity);
                    }
                })
                .setNegativeButton("取消", null)
                .create()
                .show();
    }
}
